﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using database;

namespace Registrar
{
    public partial class Form2 : Form
    {
        dbase process = new dbase();
        dbase db = new dbase();
        DataTable students = new DataTable();
        Form1 frm;
        public string sendID = "";
        
        public Form2(Form1 frm1)
        {
            InitializeComponent();
            frm = frm1;
            this.FormClosed += new FormClosedEventHandler(Form2_FormClosed);
            this.Load += new EventHandler(Form2_Load);
            radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
            button1.Click += new EventHandler(button1_Click);
            db.process_completed += new EventHandler(db_process_completed);
            button2.Click += new EventHandler(button2_Click);
            dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            process.process_completed += new EventHandler(process_process_completed);
            buttonSave.Click += new EventHandler(buttonSave_Click);
            textBoxSearch.TextChanged += new EventHandler(textBoxSearch_TextChanged);
            buttonCancel.Click += new EventHandler(buttonCancel_Click);
            buttonPrint.Click += new EventHandler(buttonPrint_Click);
        }

        void buttonPrint_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true && textBoxID.Text != "")
            {
                sendID = textBoxID.Text;
                Form3 frm3 = new Form3(this);
                frm3.ShowDialog();
            }
        }

        void buttonCancel_Click(object sender, EventArgs e)
        {
            textClear();
        }

        void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == false)
            {
                loadData();
            }
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            processData();
        }

        void process_process_completed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler eh = new EventHandler(process_process_completed);
                this.Invoke(eh, new object[] {  });
                return;
            }
           
            MessageBox.Show(buttonSave.Text+" Complete.");
            textClear();
        }
        void textClear()
        {
            textBoxID.Text = "";
            textBoxlname.Text = "";
            textBoxlstatnd.Text = "";
            textBoxmname.Text = "";
            textBoxmnum.Text = "";
            textBoxmother.Text = "";
            textBoxreligion.Text = "";
            textBoxSearch.Text = "";
            textBoxguardian.Text = "";
            textBoxgnum.Text = "";
            textBoxfnum.Text = "";
            textBoxfname.Text = "";
            textBoxfather.Text = "";
            textBoxcitizen.Text = "";
            textBoxbplace.Text = "";
            comboBoxGrade.Text = "";
            comboBoxgender.Text = "";
            textBoxaddress.Text = "";
            checkBox1.Checked = false;
            checkBox2.Checked = false;
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                for (int i = 0; i < db.data().Rows.Count; i++)
                {
                    if (radioButton1.Checked == true)
                    {
                        if (db.data().Rows[i]["id"].ToString() == dataGridView1.SelectedRows[0].Cells[0].Value.ToString())
                        {
                            textBoxID.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

                            string[] tmpName = db.data().Rows[i]["fname"].ToString().Split('~');
                            textBoxlname.Text = tmpName[0];
                            textBoxfname.Text = tmpName[1];
                            textBoxmname.Text = tmpName[2];

                            comboBoxGrade.Text = db.data().Rows[i]["grade"].ToString();
                            textBoxID.Text = db.data().Rows[i]["id"].ToString();
                            buttonSave.Text = "Add";
                            break;
                        }
                    }
                    else
                    {
                        if (db.data().Rows[i]["id"].ToString() == dataGridView1.SelectedRows[0].Cells[0].Value.ToString())
                        {
                            textBoxID.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                            textBoxlname.Text = db.data().Rows[i]["lname"].ToString();
                            textBoxfname.Text = db.data().Rows[i]["fname"].ToString();
                            textBoxmname.Text = db.data().Rows[i]["mname"].ToString();
                            comboBoxgender.Text = db.data().Rows[i]["gender"].ToString();
                            textBoxcitizen.Text = db.data().Rows[i]["citizenship"].ToString();
                            dateTimePickerbday.Value = DateTime.Parse(db.data().Rows[i]["bday"].ToString());
                            textBoxbplace.Text = db.data().Rows[i]["bplace"].ToString();
                            textBoxaddress.Text = db.data().Rows[i]["adress"].ToString();
                            textBoxreligion.Text = db.data().Rows[i]["religion"].ToString();
                            textBoxlstatnd.Text = db.data().Rows[i]["last_school_atnd"].ToString();
                            textBoxmother.Text = db.data().Rows[i]["mother_name"].ToString();
                            textBoxfather.Text = db.data().Rows[i]["father_name"].ToString();
                            textBoxguardian.Text = db.data().Rows[i]["guardian_name"].ToString();
                            string[] tmp_contact = db.data().Rows[i]["contact_number"].ToString().Split('~');


                            if (db.data().Rows[i]["birth_certificate"].ToString() == "1")
                            {
                                checkBox1.Checked = true;
                            }
                            else
                            {
                                checkBox1.Checked = false;
                            }

                            if (db.data().Rows[i]["report_card"].ToString() == "1")
                            {
                                checkBox2.Checked = true;
                            }
                            else
                            {
                                checkBox2.Checked = false;
                            }

                            try
                            {
                                textBoxmnum.Text = tmp_contact[0];
                                textBoxfnum.Text = tmp_contact[1];
                                textBoxgnum.Text = tmp_contact[2];
                            }
                            catch
                            {
                            }

                            comboBoxGrade.Text = db.data().Rows[i]["grade_lvl"].ToString();
                            textBoxID.Text = db.data().Rows[i]["id"].ToString();
                            buttonSave.Text = "Update";
                            break;
                        }
                    }
                }
            }
        }

        void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Enabled = false;
            textClear();
            if (radioButton1.Checked == true)
            {
                tbl = "payment_tbl";
                db.GenerateConnection("SELECT * FROM " + tbl + " WHERE active=0");
            }
            else
            {
                tbl = "student_profile_tbl";
                db.GenerateConnection("SELECT * FROM " + tbl);
            }
        }

        void loadData()
        {
            students = new DataTable();
            students.Columns.Add("ID NO");
            students.Columns.Add("GRADE");
            students.Columns.Add("NAME");
            string[] tmpName = new string[0];
            for (int i = 0; i < db.data().Rows.Count; i++)
            {
                if (radioButton1.Checked == true)
                {
                    if (db.data().Rows[i]["active"].ToString() == "0")
                    {
                        if (textBoxSearch.Text == "")
                        {
                            DataRow dr = students.NewRow();
                            dr["ID NO"] = db.data().Rows[i]["id"].ToString();
                            dr["GRADE"] = db.data().Rows[i]["grade"].ToString();

                            tmpName = db.data().Rows[i]["fname"].ToString().Split('~');
                            dr["NAME"] = tmpName[0] + " " + tmpName[1] + " " + tmpName[2]; ;
                            students.Rows.Add(dr);
                        }
                        else
                        {
                            tmpName = db.data().Rows[i]["fname"].ToString().Split('~');
                            if (textBoxSearch.Text.ToLower().StartsWith(tmpName[0].ToLower()) || textBoxSearch.Text.ToLower().StartsWith(tmpName[1].ToLower()))
                            {
                                DataRow dr = students.NewRow();
                                dr["ID NO"] = db.data().Rows[i]["id"].ToString();
                                dr["GRADE"] = db.data().Rows[i]["grade"].ToString();

                                tmpName = db.data().Rows[i]["fname"].ToString().Split('~');
                                dr["NAME"] = tmpName[0] + " " + tmpName[1] + " " + tmpName[2]; ;
                                students.Rows.Add(dr);
                            }
                        }
                    }
                }
                else
                {
                    string tmp = db.data().Rows[i]["lname"].ToString()+" "
                        +db.data().Rows[i]["fname"].ToString()+" "
                        +db.data().Rows[i]["mname"].ToString();
                    
                    if ((textBoxSearch.Text == db.data().Rows[i]["id"].ToString()
                            || tmp.ToLower().StartsWith(textBoxSearch.Text)) && textBoxSearch.Text != "")
                    {
                        DataRow dr = students.NewRow();
                        dr["ID NO"] = db.data().Rows[i]["id"].ToString();
                        dr["GRADE"] = db.data().Rows[i]["grade_lvl"].ToString();
                        dr["NAME"] = tmp;
                        students.Rows.Add(dr);
                    }
                }
            }
            dataGridView1.DataSource = students;
        }
        void processData()
        {
            if (radioButton1.Checked == true)
            {
                string contactNumbers = textBoxmnum.Text + "~" + textBoxfnum.Text + "~" + textBoxgnum.Text;

                int bc = 0;
                int rc = 0;
                if (checkBox1.Checked == true)
                {
                    bc = 1;
                }
                if (checkBox2.Checked == true)
                {
                    rc = 1;
                }
                
                process.connectionDatabase("UPDATE payment_tbl SET active='1' WHERE id='" + textBoxID.Text + "'");
                string[] bday = dateTimePickerbday.Value.Date.ToString().Split(' ');
                process.GenerateConnection("INSERT INTO student_profile_tbl(id,lname,fname,mname,gender,citizenship,bday,bplace,adress,grade_lvl,religion,last_school_atnd,mother_name,father_name,guardian_name,contact_number,birth_certificate,report_card) " +
                    "VALUES('" + textBoxID.Text + "','" + textBoxlname.Text + "','" + textBoxfname.Text + "','" + textBoxmname.Text + "','" + comboBoxgender.Text + "','" + textBoxcitizen.Text + "','" + bday[0] + "','" + textBoxbplace.Text + "','" + textBoxaddress.Text
                    + "','" + comboBoxGrade.Text + "','" + textBoxreligion.Text + "','" + textBoxlstatnd.Text + "','" + textBoxmother.Text + "','" + textBoxfather.Text + "','" + textBoxguardian.Text + "','" + contactNumbers + "','" + bc.ToString() + "','" + rc.ToString() + "')");
            }
            else
            {
                string contactNumbers = textBoxmnum.Text + "~" + textBoxfnum.Text + "~" + textBoxgnum.Text;

                int bc = 0;
                int rc = 0;
                if (checkBox1.Checked == true)
                {
                    bc = 1;
                }
                if (checkBox2.Checked == true)
                {
                    rc = 1;
                }

                process.GenerateConnection("UPDATE student_profile_tbl SET lname='" + textBoxlname.Text + "',fname='" + textBoxfname.Text + "',mname='" + textBoxmname.Text +
                    "',gender='" + comboBoxgender.Text + "',citizenship='" + textBoxcitizen.Text + "',bday='" + dateTimePickerbday.Value.Date.ToString() + "',bplace='" + textBoxbplace.Text +
                    "',adress='" + textBoxaddress.Text + "',grade_lvl='" + comboBoxGrade.Text + "',religion='" + textBoxreligion.Text + "',last_school_atnd='" + textBoxlstatnd.Text +
                    "',mother_name='" + textBoxmother.Text + "',father_name='" + textBoxfather.Text + "',guardian_name='" + textBoxguardian.Text + "',contact_number='" + contactNumbers +
                    "',birth_certificate='" + bc.ToString() + "',report_card= '" + rc.ToString() + "' WHERE id='"+textBoxID.Text+"'");

            }
        }

        void db_process_completed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler eh = new EventHandler(db_process_completed);
                this.Invoke(eh, new object[] { db.data() });
                return;
            }
            loadData();
            dataGridView1.Enabled = true;
        }

        void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        string tbl = "";
        void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.Enabled = false;
            textClear();
            if (radioButton1.Checked == true)
            {
                tbl = "payment_tbl";
                db.GenerateConnection("SELECT * FROM " + tbl + " WHERE active=0");
            }
            else
            {
                tbl = "student_profile_tbl";
                db.GenerateConnection("SELECT * FROM " + tbl);
            }
        }

        void Form2_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm.active = false;
        }

        
    }
}
